﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.IO;
using System.Windows.Forms;
using System.Security.Cryptography;
using System.Globalization;

namespace Clinica_Frba
{
    public class Utility
    {
        private static Utility instance;
        private string server;
        private string user;
        private string pass;
        private string dataBase;
        private string appFecha;
        private string appHora;
        private bool loggedIn = false;
        private string userName;
        private int    idRol;

        private Utility() { }

        public static Utility getInstance()
        {
            if (instance == null)
                instance = new Utility();

            return instance;
        }

        public void loadConfig() 
        {
            XmlReader reader;
            string param;
            string value;

            try
            {
                reader = XmlReader.Create("appConfig.xml");

                // Parsear XML
                while (reader.Read())
                {
                    if (reader.NodeType == XmlNodeType.Element && reader.Name == "add")
                    {
                        param = reader.GetAttribute("param"); //Nombre del parametro
                        value = reader.GetAttribute("value"); //Valor

                        //Guardar parametros de configuracion
                        switch (param)
                        {
                            case "Server":
                                server = value;
                                break;

                            case "DataBase":
                                dataBase = value;
                                break;

                            case "User":
                                user = value;
                                break;

                            case "Password":
                                pass = value;
                                break;

                            case "FechaApp":
                                appFecha = value;
                                break;

                            case "HoraApp":
                                appHora = value;
                                break;
                        }
                    }
                }
            }
            catch (System.IO.FileNotFoundException)
            {
                MessageBox.Show("No se encontro el archivo de configuracion. Verifique que se encuentre en el mismo directorio que la aplicacion", "Error");
                Application.Exit();
            }
            catch (Exception)
            {
                MessageBox.Show("Ocurrio un error al cargar el archivo de configuracion", "Error");
                Application.Exit();
            }
        }

        public string getConnectionString()
        {   //Retorna string de conexion a la BD
            return ("Data Source="+server+";Initial Catalog="+dataBase+";User ID="+user+";Password="+pass);
        }

        public string getAppDate()
        {
            return appFecha;
        }

        public DateTime getAppDateTime()
        {
            return DateTime.ParseExact(getAppDate()+getAppHora(), "yyyyMMddHH:mm:ss", CultureInfo.InvariantCulture);
        }

        public string getAppHora()
        {
            return appHora;
        }

        public void setLogin(bool flag_set)
        {
            loggedIn = flag_set;
        }

        public bool isLoggedIn()
        {
            return loggedIn;
        }

        public void setUser(string userName)
        {
            this.userName = userName;
        }

        public string getUserName()
        {
            return userName;
        }

        public void setIdRol(int idRol)
        {
            this.idRol = idRol;
        }

        public int getIdRol()
        {
           return idRol;
        }

        public string sha256Encrypt(string code)
        {
            string codeEncrypted = string.Empty;
            byte[] bytes = Encoding.UTF8.GetBytes(code);
            SHA256Managed sha256 = new SHA256Managed();
            byte[] hash = sha256.ComputeHash(bytes);

            foreach (byte x in hash)
            {
                codeEncrypted += String.Format("{0:x2}", x);
            }

            return codeEncrypted;
        }

        public bool isNumber(string stringNum)
        {
            int number1 = 0;
            return (int.TryParse(stringNum, out number1));
        }
    }
}
